package cn.kkcxy.blog.server.controller.admin;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.kkcxy.blog.common.constant.ExceptionMessageConstant;
import cn.kkcxy.blog.core.dto.admin.ManageGetUserListDTO;
import cn.kkcxy.blog.core.exception.backend.SystemBusinessException;
import cn.kkcxy.blog.core.result.ResponseEntity;
import cn.kkcxy.blog.core.vo.admin.system.ManageGetUserListVO;
import cn.kkcxy.blog.core.vo.admin.system.UserDetailsVO;
import cn.kkcxy.blog.system.service.SystemService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * @BelongsProject: kkcxy-blog
 * @BelongsPackage: cn.kkcxy.blog.server.controller.admin
 * @Author: kkcxy
 * @CreateTime: 2024-11-17 00:20
 * @Description: 系统管理
 * @Version: 1.0
 */
@Tag(name = "管理端-系统管理")
@Slf4j
@RestController
@RequestMapping("/backend/system")
public class SystemController {

    @Resource
    private SystemService systemService;

    /**
     * 获取用户列表
     */
    @SaCheckPermission(value = { "system::user::list" })
    @Operation(summary = "获取用户列表")
    @PostMapping("/management/user/get-list")
    public ResponseEntity<ManageGetUserListVO> getAllOfUser(@RequestBody ManageGetUserListDTO manageGetUserListDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("[管理端] 获取用户列表 请求参数: {}", manageGetUserListDTO);
            }
            return ResponseEntity.success(systemService.getAllOfUser(manageGetUserListDTO));
        } catch (SystemBusinessException exception) {
            log.error("[管理端] 获取用户列表 [ERROR]: {}", exception.getMessage(), exception);
            return ResponseEntity.fail(ExceptionMessageConstant.BACKEND_GET_USER_LIST_ERROR);
        }
    }

    /**
     * 获取用户详情
     */
    @SaCheckPermission(value = { "system::user::details" })
    @Operation(summary = "获取用户详情")
    @GetMapping("/management/user/get-details/{userId}")
    public ResponseEntity<UserDetailsVO> getUserDetails(@PathVariable String userId) {
        try {
            if (log.isInfoEnabled()) {
                log.info("[管理端] 获取用户详情 userId: {}", userId);
            }
            return ResponseEntity.success(systemService.getUserDetails(userId));
        } catch (SystemBusinessException exception) {
            log.error("[管理端] 获取用户详情 [ERROR]: {}", exception.getMessage(), exception);
            return ResponseEntity.fail(ExceptionMessageConstant.BACKEND_GET_USER_LIST_ERROR);
        }
    }
}
